package controllers;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import model.Movie;
import play.*;
import play.mvc.*;
import views.html.*;
import model.*;
import play.data.*;
import controllers.*;

public class Application extends Controller {

	
	
    public static Result index(String username) {
    	List<Movie>movieList;
    	DatabaseController dbController = new DatabaseController();
    	movieList = dbController.GetMoviesFromDB();
    	
    	Integer counter = dbController.GetPeopleCount();
        
 		return ok(index.render(movieList, dbController.GetPersonFromDB(username),counter));
    }
    
    public static Result login()
    {
    	DatabaseController dbController = new DatabaseController();
    	
    	return ok(
    	        login.render(Form.form(Login.class),null)
    	    );
    }
    
    public static Result authenticate() {
        Form<Login> loginForm = Form.form(Login.class).bindFromRequest();
        Login l=loginForm.get();
        DatabaseController dbController = new DatabaseController();
        
        if(dbController.GetPersonFromDB(l.username)!= null)
        {
        	return ok(login.render(Form.form(Login.class),"Sorry " + l.username + ", you have already filled out this survey"));
        }
        else
        {
        dbController.InsertPersonToDB(new Person(l.username));
        
        return redirect("/survey/"+l.username);
        		//index(l.username);
        }
    }
    
    /*public static Result evaluate()
    {

        Map<String,String[]> parameters = request().body().asFormUrlEncoded();
        String movieid = parameters.get("movieid")[0];
        String stars = parameters.get("stars")[0];
        String user = parameters.get("uname")[0];
        DatabaseController dbController = new DatabaseController();
        Person p=dbController.GetPersonFromDB(user);
        Evaluation eval = dbController.GetSingleEvaluationFromDB(Integer.parseInt(movieid), p.Id);
        if(eval!=null)
        {
        	if(stars.equals("null"))
        		dbController.UpdateEvaluationToDB(eval.Id, null);
        	else
        		dbController.UpdateEvaluationToDB(eval.Id, Integer.parseInt(stars));
        }
        else{
        	if(stars.equals("null")){
        		dbController.InsertEvaluationToDB(new Evaluation(p.Id, Integer.parseInt(movieid), null));
        	}
        	else{
        		dbController.InsertEvaluationToDB(new Evaluation(p.Id, Integer.parseInt(movieid), Integer.parseInt(stars)));
        	}
        }
        return ok();
    	
    }*/


    public static Result submitEval(String username)
    {
        Map<String,String[]> parameters = request().body().asFormUrlEncoded();
        DynamicForm f=Form.form().bindFromRequest();
        Map<String,String> p=f.data();
        for(int i=1;i<=p.size();i++)
        {
        	DatabaseController dbController = new DatabaseController();
           Person per=dbController.GetPersonFromDB(username);
            Evaluation eval = dbController.GetSingleEvaluationFromDB(i, per.Id);
            if(eval!=null)
            {
            	if(f.get("StarList"+i).equals("null"))
            		dbController.UpdateEvaluationToDB(eval.Id, null);
            	else
            	dbController.UpdateEvaluationToDB(eval.Id, Integer.parseInt(f.get("StarList"+i)));
            }
            else
            {
            	if(f.get("StarList"+i).equals("null")){
            		dbController.InsertEvaluationToDB(new Evaluation(per.Id,i, null));
            	}
            	else
            	{
            dbController.InsertEvaluationToDB(new Evaluation(per.Id, i, Integer.parseInt(f.get("StarList"+i))));
            	}
            	}
        
        }
        System.out.println(p.size());

        return ok(submit.render());
    	
    }

    public static class Login {

		public String username;
		//  public String email;
		 //   public String password;		
	}
    
    

}


